package com.zz.hhbl.ui

import android.view.LayoutInflater
import android.widget.SeekBar
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import com.zz.framework.core.base.BaseFragment
import com.zz.framework.core.ext.onClick
import com.zz.framework.core.ext.viewBinding
import com.zz.framework.core.utils.LogUtils
import com.zz.framework.widget.musicplayer.MusicPlayerView
import com.zz.hhbl.R
import com.zz.hhbl.databinding.FragmentMusicPlayerTestBinding
import com.zz.hhbl.databinding.ViewAudioPalyerBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/2/20 17:59.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
@AndroidEntryPoint
class MusicPlayerTestFragment : BaseFragment(R.layout.fragment_music_player_test) {
    private val viewModel: CommonViewModel by viewModels()
    private val binding by viewBinding<FragmentMusicPlayerTestBinding>()
    var speed = 1f
    var isSeekBarChanging = false
    var totalDuration = 0
    var currentPosition = 0
    var isPlay = true
    override fun initView() {
        val playBinding = ViewAudioPalyerBinding.inflate(LayoutInflater.from(getBaseActivity()))
        binding?.musicPlayer?.let { player ->
            player.addView(playBinding.root)
            player.setIconDrawable(ContextCompat.getDrawable(requireContext(), R.mipmap.ic_logo))
            playBinding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
                override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {

                }

                override fun onStartTrackingTouch(p0: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar) {
                    player.seekToMusic((seekBar.progress * totalDuration / 100.0).toInt())
                }
            })
            player.setOnMusicStateListener(object : MusicPlayerView.OnMusicStateListener {
                override fun onPrepared(duration: Int) {
                }

                override fun onError(what: Int, extra: Int) {
                }

                override fun onInfo(what: Int, extra: Int) {
                }

                override fun onMusicPlayComplete() {
                    //播放完成
                    playBinding.ivPlayOrPause.setImageResource(R.drawable.vector_pause)
                }

                override fun onProgressUpdate(duration: Int, currentPos: Int) {
                    if (isSeekBarChanging.not()) {
                        playBinding.seekBar.progress = 100 * currentPos / duration
                    }
                    currentPosition = currentPos
                    totalDuration = duration
                    LogUtils.error("currentPosition:$currentPosition  totalDuration:$totalDuration")
                    playBinding.tvCurrentTime.text = getGapTime(currentPos.toLong())
                    playBinding.tvTotalTime.text = getGapTime(totalDuration.toLong())
                }

                override fun onSeekComplete() {
                }

                override fun onHeadsetPullOut() {
                }
            })
            playBinding.ivPlayOrPause.onClick {
                if (isPlay) {
                    isPlay = false
                    player.pausePlayMusic()
                    playBinding.ivPlayOrPause.setImageResource(R.drawable.vector_play)
                } else {
                    isPlay = true
                    player.resumePlayMusic()
                    playBinding.ivPlayOrPause.setImageResource(R.drawable.vector_pause)
                }
            }
            playBinding.ivPlayback15s.onClick {
                player.seekToMusic(currentPosition-15_000)
            }
            playBinding.ivPalyForward15s.onClick {
                player.seekToMusic(currentPosition+15_000)
            }
            playBinding.ivSpeedPlay.onClick {
                if (speed == 1f) {
                    speed = 1.5f
                } else if (speed == 1.5f) {
                    speed = 0.75f
                } else {
                    speed = 1f
                }
                binding?.musicPlayer?.setPlaySpeed(speed)
            }
            player.startPlayMusic("http://music.163.com/song/media/outer/url?id=1908673805.mp3")
            player.setPlaySpeed(speed)
        }
        binding?.btnSpeed?.onClick {
            speed++
            binding!!.musicPlayer.setPlaySpeed(speed)
        }
    }


    fun getGapTime(time: Long): String {
        val hours = time / (1000 * 60 * 60)
        val minutes = (time - hours * (1000 * 60 * 60)) / (1000 * 60)
        val second = (time - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000
        var diffTime = "00:00:00"
        diffTime = if (minutes < 10) {
            "$hours:0$minutes"
        } else {
            "$hours:$minutes"
        }
        diffTime = if (second < 10) {
            "$diffTime:0$second"
        } else {
            "$diffTime:$second"
        }
        return diffTime
    }
}